{include file="header/index" /}
		<div class="container whitebg">
			<form class="form-horizontal mt40" id="operation" method="post" action="" enctype="multipart/form-data">
				<div class="form-group displaycenter upload_header">
					<label class="col-sm-2 control-label">头像</label>
					<div class="col-sm-10 displaycenter">
						<div class="uploadheader">
							<div class="btn btn-default pointer">选择头像</div>
							<input type="file" name="avatar_img" id="avatar_img" class="webuploader-element-invisible" multiple="multiple" onchange="uploadFile()" accept="image/jpg,image/jpeg,image/png">
						</div>
						<img id="img" src="{if condition='(empty($admin))'}__IMG__/head_default.gif{else /}{:Config('IMAGES_PATH')}{$admin.avatar_img}{/if}" class="upload_IMG img-circle" />
					</div>
				</div>
				<div class="form-group">
					<label class="col-sm-2 control-label">帐号</label>
					<div class="col-sm-4">
						<input type="text" class="form-control" placeholder="请输入登录账号" id="user_account" name="user_account" {if condition='(!empty($admin.account))'}readonly="readonly"{/if} id="account" value="{if condition='(empty($admin))'}{else /}{$admin.account}{/if}">
					</div>
				</div>
				<div class="form-group">
					<label class="col-sm-2 control-label">密码</label>
					<div class="col-sm-4">
						<input type="hidden" name="user_strict" value="{if condition='(empty($admin))'}{else /}{$admin.strict}{/if}">
						<input type="text" placeholder="请输入登录密码" maxlength="20" minlength="6" class="form-control" name="user_password" id="password" value="">
					</div>
				</div>
				<div class="form-group">
					<label class="col-sm-2 control-label">邮箱</label>
					<div class="col-sm-4">
						<input type="text" class="form-control" placeholder="请输入邮箱" name="user_email" id="email" value="{if condition='(empty($admin))'}{else /}{$admin.email}{/if}">
					</div>
				</div>
				<div class="form-group">
					<label for="company_name" class="col-sm-2 control-label">行政区域名称</label>
					<div class="col-sm-4">
						<input type="text" class="form-control" name="company_name" id="company_name" value="{if condition='(empty($data))'}{else /}{$data.company_name}{/if}">
					</div>
				</div>
				<div class="form-group add_center">
					<label class="col-sm-2 control-label">负责区域</label>
					<?php if(isset($p_id)){ ?><input type="hidden" id="p_id" name="province_id" value="{$p_id}"><input
						type="hidden" name="province_name" value="{$p_name}"><?php } ?>
					<div class="col-sm-8 choosecity">
						<select id="Area" name="area_id[]" onmousedown="moveArea()" onkeydown="moveArea()" onchange="checkArea(this)" class="form-control administrative">
							<option value="">请选择</option>
							<?php if(isset($Area)){ ?>
							{volist name="Area" id="Ar"}
							{if condition='((isset($data)) AND ($Ar.id == $data.area_id))'}
							<option value="{$Ar.id}" {if condition='((isset($data)) AND ($Ar.id == $data.area_id))'}selected{else /}{/if}>{$Ar.area_name}</option>
							{/if}
							{/volist}
							<?php } ?>
						</select>
						<input type="hidden" value="1">
						<div class="btn btn-default" onclick="addselect('',this)"><i class="glyphicon glyphicon-plus"></i></div>
					</div>
				</div>


				<div class="form-group">
					<label for="address" class="col-sm-2 control-label">详细地址</label>
					<div class="col-sm-4">
						<input type="text" class="form-control" id="address" name="address" value="{if condition='(empty($data))'}{else /}{$data.address}{/if}">
					</div>
				</div>
				<div class="form-group">
					<label for="name" class="col-sm-2 control-label">联系人</label>
					<div class="col-sm-4">
						<input type="text" class="form-control" id="name" name="name" value="{if condition='(empty($data))'}{else /}{$data.name}{/if}">
					</div>
				</div>
				<div class="form-group">
					<label for="phone" class="col-sm-2 control-label">联系电话</label>
					<div class="col-sm-4">
						<input type="text" class="form-control" id="phone" name="phone" value="{if condition='(empty($data))'}{else /}{$data.phone}{/if}">
					</div>
				</div>
				{if condition='((!empty($data)))'}
				<div class="form-group">
					<label class="col-sm-2 control-label">区域状态</label>
					<div class="col-sm-4">
						<label class="radio-inline"><input type="radio" name="status" id="inlineRadio1" value="1" {if condition='((!empty($data)) AND ($data.status == 1))'}checked{else /}checked{/if}>正常</label>
						<label class="radio-inline"><input type="radio" name="status" id="inlineRadio2" value="0" {if condition='((!empty($data)) AND ($data.status == 0))'}checked{/if}>冻结</label>
					</div>
				</div>
				{/if}
				<div class="form-group">
					<label for="Remarks" class="col-sm-2 control-label">备注</label>
					<div class="col-sm-5">
						<textarea class="form-control" id="Remarks" name="note" rows="3" placeholder="请输入备注信息" onkeydown="checkMaxInput(this,100)" onkeyup="checkMaxInput(this,100)" onfocus="checkMaxInput(this,100)" onblur="checkMaxInput(this,100);resetMaxmsg()">{if condition='(empty($data))'}{else /}{$data.note}{/if}</textarea>
					</div>
				</div>
				<input type="hidden" name="a_id" value="{if condition='(empty($data.a_id))'}{else /}{$data.a_id}{/if}">
				<input type="hidden" name="operation" value="{if condition='(empty($o_id))'}{else /}{$o_id}{/if}">
			</form>
			<form class="form-inline btnlists">
				<button type="button" id="but" class="btn btn-primary">保存修改</button>
				<a class="btn btn-default" href="{:url('operations/index')}">返回</a>
			</form>
		</div>
	</body>

</html>
<script type="text/javascript">

    function checkArea(obj) {
        var area = $(obj).find("option:selected").val();
        $.get("{:url('Operations/checkArea')}",{area:area},function (data) {
            if(data){
                top.layer.msg(data.msg, {icon: 2,time: 2000},function () {
					if($(obj).next("input[type='hidden']").val()==1){
                        $(".add_center select").val("")
                	}
                	delselect1(obj);
                });
            }
        })
    }

    //行政中心绑定
    function uploadFile() {
        var file = document.getElementById("avatar_img").files[0];
        var reader = new FileReader();
        reader.onload = function(e) {
            img.src = this.result;
        };
        reader.readAsDataURL(file); //Base64
    }
    function bindAccountOperation(id) {
        $("#o_id").val(id);
        $("#img").attr('src',"__IMG__/head_default.gif");
        $("#form_operation")[0].reset();
        $.post("{:url('Operations/bindAccountAdministrative')}",{o_id:id},function (data) {
            if (data.code == 1){
                $("#user_account").css('display','none');
                $("#img").attr('src',"{:Config('IMAGES_PATH')}"+data.data.avatar_img);
                $("#account").val(data.data.account);
                $("#email").val(data.data.email);
                $("#user_name").val(data.data.name);
                $("#user_phone").val(data.data.phone);
                $("#address").val(data.data.address);
            }else {
                $("#user_account").css('display','');
            }
        });
        $("#user_account").click(function () {
            var formData = new FormData($( "#form_operation" )[0]);
            $.ajax({
                url:  "{:url('Operations/bindAccountAdministrative')}",
                type: 'POST',
                data: formData,
                async: false,
                cache: false,
                contentType: false,
                processData: false,
                dataType : 'json',
                success: function (returndata) {
                    if(returndata.code == '1'){
                        top.layer.msg(returndata.msg, {icon: 1,time: 2000},function(){history.back();});
                    }else{
                        top.layer.msg(returndata.msg, {icon: 2,time: 2000},function(){history.back();});
                    }

                },error: function (returndata) {
                    top.layer.msg('数据异常', {icon: 2,time: 2000});
                    return false;
                }
            });

        });
    }

	//提交
	$("#but").click(function () {
		if ($("#user_account").val() == ''){
            top.layer.msg("请绑定账号", {icon: 7,time: 3000},function(){});
			return false;
		}
		$("#province_name").val($("#province option:selected").text());
		var operation = new FormData($("#operation")[0]);
		console.log(operation);
		$.ajax({
			url:  "{:url('Operations/doEditOperation')}",
			type: 'POST',
			data: operation,
			async: false,
			cache: false,
			contentType: false,
			processData: false,
			dataType : 'json',
			success: function (returndata) {
				if(returndata.code == '1'){
                    top.layer.msg(returndata.msg, {icon: 1,time: 2000},function(){history.back();});
				}else{
                    top.layer.msg(returndata.msg, {icon: 2,time: 2000},function(){});
				}

			},error: function (returndata) {
                top.layer.msg('数据异常', {icon: 2,time: 2000});
				return false;
			}
		});
    });

	//负责多区域
    addArea();
	function addArea() {
        {if condition='((isset($data.area)))'}
			{foreach $data.area as $k => $vo}
        		{if condition='($k != 0)'}
        			addselect({$vo.area_id});
        		{/if}
			{/foreach}
        {/if}
    }

    //选择地址
    function moveArea() {
        var province = $("#p_id").val();
		$.get("{:url('Operations/Area')}",{province:province},function (data) {
		 $("#Area").empty();
		 $("#Area").append($("<option>").val("").text("请选择"));
			for (var i = 0;i<data.length;i++){
				var option = $("<option>").val(data[i].id).text(data[i].area_name);
				$("#Area").append(option);
			}
		})
	}


	//多行文本输入框剩余字数计算
	function checkMaxInput(obj, maxLen) {
		if(obj == null || obj == undefined || obj == "") {
			return;
		}
		if(maxLen == null || maxLen == undefined || maxLen == "") {
			maxLen = 100;
		}

		var strResult;
		var $obj = $(obj);
		var newid = $obj.attr("id") + 'msg';

		if(obj.value.length > maxLen) { //如果输入的字数超过了限制
			obj.value = obj.value.substring(0, maxLen); //就去掉多余的字
			strResult = '<span id="' + newid + '" class=\'Max_msg\' ><br/>剩(' + (maxLen - obj.value.length) + ')字</span>'; //计算并显示剩余字数
		} else {
			strResult = '<span id="' + newid + '" class=\'Max_msg\' ><br/>剩(' + (maxLen - obj.value.length) + ')字</span>'; //计算并显示剩余字数
		}

		var $msg = $("#" + newid);
		if($msg.length == 0) {
			$obj.after(strResult);
		} else {
			$msg.html(strResult);
		}
	}

	//清空剩除字数提醒信息
	function resetMaxmsg() {
		$("span.Max_msg").remove();
	}

	//增加 删除
	function addselect(area,obj) {
        var province = $("#p_id").val();
        var administrative_id = [];
        $("select[name='administrative']").each(function(){
            administrative_id.push($(this).val());
        });
        $.get("{:url('Operations/Area')}",{province:province},function (data) {
            var html = '<div class="form-group form-more">';
            html += '<label class="col-sm-2 control-label"></label>';
			html += '<div class="col-sm-8 choosecity">';

                html += '<select id="Area" name="area_id[]" onchange="checkArea(this)" class="form-control administrative">';
                html += '<option value="">请选择</option>';
                for (var i = 0; i < data.length; i++) {
                    //去掉已经选择的区域
                    if ($.inArray(data[i].id.toString(),administrative_id) == -1) {
                    	html += '<option value="' + data[i].id + '"';
                        if (data[i].id == area){
                            html += 'selected>' + data[i].area_name + '</option>';
						}else {
                            html += '>' + data[i].area_name + '</option>';
						}
                    }
				}
                html += '</select>';
                html +='<input type="hidden" value="2">'
                html += '<div class="btn btn-default" onclick="delselect(this)"><i class="glyphicon glyphicon-minus"></i></div>';
                html += '</div>';
                html += '</div>';
            /**
			 *判断是否选择城市为空
             */

             $(".add_center").after(html);

        })
	}

	function delselect(obj){
		$(obj).parents(".form-group").remove();
	}
    function delselect1(obj){
                    $(obj).parents(".form-more").remove();
                }

   window.onload=function(){
		var address=$("#Area").val();
		if(address==""){
			moveArea();
		}
	}
</script>
